﻿
@{
    ViewBag.Title = "InfoView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
@section head{
    <script src="~/Scripts/exportJs.js"></script>
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>前期测算信息</legend>
</fieldset>
<div>
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">主项目选择</label>
            <div class="layui-input-inline" style="width:500px;">
                <select name="ProjectId" lay-verify="required" lay-search lay-filter="ProjectSelFilter" id="ProjectSel">
                    <option value=""></option>
                    @for (int i = 0; i < ViewBag.ProjectList.Count; i++)
                    {
                        <option value="@ViewBag.ProjectList[i]["ProjectId"]">@ViewBag.ProjectList[i]["Name"]|@ViewBag.ProjectList[i]["ProjectCode"]|@ViewBag.ProjectList[i]["Type"]</option>
                    }
                </select>
            </div>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增</button>
        </div>
    </form>
</div>
@*<table id="demo" lay-filter="SubConTableList"></table>*@
<div class="layui-tab" lay-filter="tabFilter">
    <ul class="layui-tab-title">
        <li class="layui-this">前期测算表</li>
        <li>主业结算表</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <table id="demo" lay-filter="SubConTableList"></table>
        </div>
        <div class="layui-tab-item">
            <table id="JSTable" lay-filter="JSTableFilter"></table>
        </div>
    </div>
</div>


<div id="DivAdd" style="display:none;margin-top:5px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">新增测算表</label>
            <div class="layui-input-block">
                <input type="text" id="InputDivName" class="layui-input" placeholder="输入测算表名称" style="width:300px;float:left;">
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnDivAdd">确认新增</button>
            </div>
        </div>
    </form>
</div>

<script type="text/template" id="LayerProgBar">
    <div class="layui-progress layui-progress-big" lay-showPercent="true" lay-filter="progressBar">
        <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
    </div>
</script>

<script type="text/javascript">
    var table;
    var TabIndex = 0;
    layui.use(['form', 'table', 'upload', 'layer', 'element'], function () {
        var element = layui.element;
        var form = layui.form;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var index;
        var layerCopyIndex;
        var ProjectType = "";
        var ProjectName = "";
        var ProjectId = "";
        var ProjectCode = "";

        element.on('tab(tabFilter)', function (data) {
            //index从0开始
            TabIndex = data.index;
        });

        $("#BtnAdd").on("click", function () {
            if (ProjectId == "") {
                return;
            }
            layerCopyIndex = layer.open({
                type: 1,
                area: ['550px', '110px'],
                content: $("#DivAdd"), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        });

        $("#BtnDivAdd").on("click", function () {
            var tempname = $("#InputDivName").val();
            tempname = tempname.replace(/\s*/g, "");
            if (tempname == "") {
                return;
            }


            if (TabIndex == 0) {
                //表示主业前期测算表新增
                $.ajax({
                    type: "POST",
                    url: "/Estimate/Add",
                    async: false,
                    data: { "ProjectType": ProjectType, "ProjectId": ProjectId, "Name": tempname },
                    success: function (msg) {
                        if (msg.result == "OK") {
                            layer.msg('添加成功"', { icon: 6, time: 1000 });
                            table.reload("demo", {
                                //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                            });
                            layer.close(layerCopyIndex);
                        } else {
                            alert(msg.result);
                        }
                    }
                });
            }

            if (TabIndex == 1) {
                //表示主业结算表新增
                $.ajax({
                    type: "POST",
                    url: "/Estimate/Add",
                    async: false,
                    data: { "ProjectType": "主业结算", "ProjectId": ProjectId, "Name": tempname },
                    success: function (msg) {
                        if (msg.result == "OK") {
                            layer.msg('添加成功"', { icon: 6, time: 1000 });
                            table.reload("JSTable", {
                                //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                            });
                            layer.close(layerCopyIndex);
                        } else {
                            alert(msg.result);
                        }
                    }
                });
            }

        });

        form.on('select(ProjectSelFilter)', function (data) {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象

            ProjectId = data.value;
            if (data.value == "") {
                return;
            }
            //下面需要先查工程类型
            
            $.ajax({
                type: "POST",
                url: "/Project/QueryProjectInfo",
                async: false,
                data: { "ProjectId": data.value },
                success: function (msg) {
                    if (msg.result == "OK") {
                        table.reload("demo", {
                            where: {
                                SortName: "PreEstId", Order: "Desc", Condition: " and IsDelete<>1 and Type='" + msg.type + "' and ProjectId=" + data.value
                            }                          
                        });
                        table.reload("JSTable", {
                            where: {
                                SortName: "PreEstId", Order: "Desc", Condition: " and IsDelete<>1 and Type='主业结算' and ProjectId=" + data.value
                            }
                        });
                        ProjectType = msg.type;
                        ProjectName = msg.Name;
                    } else {
                        alert(msg.result);
                        return;
                    }
                }
            });
        });


        //主业前期测算表
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/EstimateViewTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PreEstId", Order: "Desc", Condition: " and 1=0" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Name', title: '前期测算表名称', width: 250, edit: 'text' }
                , {
                    field: "operation", title: "操作", width: 180, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });


        //鼠标双击事件回调
        table.on('rowDouble(SubConTableList)', function (obj) {
            var data = obj.data;

        });

        table.on("tool(SubConTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                //parent.layui.index.openTabsPage('/SubContract/SubConInfoEditView?SubContractId=' + data.SubContractId, '分包合同编辑');
                var type = $("#ProjectSel").find("option:selected").text();


                var tempForm = document.createElement("form");
                tempForm.id = "tempForm1";
                tempForm.method = "post";
                tempForm.action = "/Estimate/EditIndex";
                tempForm.target = "_blank"; //打开新页面

                var hideInput1 = document.createElement("input");
                hideInput1.type = "hidden";
                hideInput1.name = "PreEstId"; 
                hideInput1.value = data.PreEstId; 
                var hideInput2 = document.createElement("input");
                hideInput2.type = "hidden";
                hideInput2.name ="ProjectName";
                hideInput2.value = ProjectName;
                var hideInput3 = document.createElement("input");
                hideInput3.type = "hidden";
                hideInput3.name = "ProjectType";
                hideInput3.value = ProjectType;
                var hideInput4 = document.createElement("input");
                hideInput4.type = "hidden";
                hideInput4.name = "PreEstName";
                hideInput4.value = data.Name;

                tempForm.appendChild(hideInput1);
                tempForm.appendChild(hideInput2);
                tempForm.appendChild(hideInput3);
                tempForm.appendChild(hideInput4);

                if (document.all) {
                    tempForm.attachEvent("onsubmit", function () { });        //IE
                } else {
                    var subObj = tempForm.addEventListener("submit", function () { }, false);    //firefox
                }
                document.body.appendChild(tempForm);
                if (document.all) {
                    tempForm.fireEvent("onsubmit");
                } else {
                    tempForm.dispatchEvent(new Event("submit"));
                }
                tempForm.submit();
                document.body.removeChild(tempForm);



            }

            //前期测算删除
            if (layEvent == 'TableDel') {
                layer.confirm('确实删除：' + data.Name, { icon: 3, title: '提示' }, function (index) {
                    //do something
                    $.ajax({
                        type: "POST",
                        url: "/Estimate/EstMainlyDel",
                        async: false,
                        data: { "PreEstId": data.PreEstId },
                        success: function (msg) {
                            if (msg.result == "OK") {
                                parent.layer.msg('删除成功', { icon: 6, time: 1000 });
                                table.reload("demo", {
                                    where: {
                                        SortName: "PreEstId", Order: "Desc", Condition: " and IsDelete<>1 and Type='主业' and ProjectId=" + ProjectId
                                    }
                                });
                            } else {
                                alert(msg.result);
                            }
                        }
                    });
                    layer.close(index);
                });
            }

        });

        table.on('edit(SubConTableList)', function (obj) {
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  

            var data = obj.data;
            var field = obj.field;

            //下面更新数据
            $.ajax({
                type: "POST",
                url: "/Estimate/EstMainlyNameUpdate",
                async: false,
                data: { "PreEstId": data.PreEstId, "Name": data.Name },
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('demo');

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        ////////////////////////////////////////////////////////////////////////////////////
        //主业结算表
        table.render({
            elem: '#JSTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/EstMainlyJSTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PreEstId", Order: "Desc", Condition: " and 1=0" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Name', title: '结算表名称', width: 250, edit: 'text' }
                , {
                    field: "operation", title: "操作", width: 180, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });


        table.on("tool(JSTableFilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                //parent.layui.index.openTabsPage('/SubContract/SubConInfoEditView?SubContractId=' + data.SubContractId, '分包合同编辑');
                var type = $("#ProjectSel").find("option:selected").text();


                var tempForm = document.createElement("form");
                tempForm.id = "tempForm1";
                tempForm.method = "post";
                tempForm.action = "/Estimate/EditIndex";
                tempForm.target = "_blank"; //打开新页面

                var hideInput1 = document.createElement("input");
                hideInput1.type = "hidden";
                hideInput1.name = "PreEstId";
                hideInput1.value = data.PreEstId;
                var hideInput2 = document.createElement("input");
                hideInput2.type = "hidden";
                hideInput2.name = "ProjectName";
                hideInput2.value = ProjectName;
                var hideInput3 = document.createElement("input");
                hideInput3.type = "hidden";
                hideInput3.name = "ProjectType";
                hideInput3.value = "主业结算";
                var hideInput4 = document.createElement("input");
                hideInput4.type = "hidden";
                hideInput4.name = "PreEstName";
                hideInput4.value = data.Name;

                tempForm.appendChild(hideInput1);
                tempForm.appendChild(hideInput2);
                tempForm.appendChild(hideInput3);
                tempForm.appendChild(hideInput4);

                if (document.all) {
                    tempForm.attachEvent("onsubmit", function () { });        //IE
                } else {
                    var subObj = tempForm.addEventListener("submit", function () { }, false);    //firefox
                }
                document.body.appendChild(tempForm);
                if (document.all) {
                    tempForm.fireEvent("onsubmit");
                } else {
                    tempForm.dispatchEvent(new Event("submit"));
                }
                tempForm.submit();
                document.body.removeChild(tempForm);



            }

            if (layEvent == 'TableDel') {
                layer.confirm('确实删除：' + data.Name, { icon: 3, title: '提示' }, function (index) {
                    //do something
                    $.ajax({
                        type: "POST",
                        url: "/Estimate/EstMainlyJSDel",
                        async: false,
                        data: { "PreEstId": data.PreEstId },
                        success: function (msg) {
                            if (msg.result == "OK") {
                                parent.layer.msg('删除成功', { icon: 6, time: 1000 });
                                table.reload("JSTable", {
                                    where: {
                                        SortName: "PreEstId", Order: "Desc", Condition: " and IsDelete<>1 and Type='主业结算' and ProjectId=" + ProjectId
                                    }
                                });
                            } else {
                                alert(msg.result);
                            }
                        }
                    });
                    layer.close(index);
                });
            }

        });

        table.on('edit(JSTableFilter)', function (obj) {
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  

            var data = obj.data;
            var field = obj.field;

            //下面更新数据
            $.ajax({
                type: "POST",
                url: "/Estimate/EstMainlyJSNameUpdate",
                async: false,
                data: { "PreEstId": data.PreEstId, "Name": data.Name },
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('JSTable');

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });




    });

  


</script>

